package people.paper.tv.utils.ui;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.skyworth.ui.define.SkyTextSize;
import com.skyworth.util.Util;

import people.paper.tv.R;

/**
 * Created by wzh on 2017/6/17.
 * Mall error view
 */


public class ServerErrorLayout extends FrameLayout {
    private TextView mTipsTxt1 = null;
    private Button mConnectBtn = null;
    private boolean hasClick = false;
    private TextView mShowFocus = null;
    private ReloadCallback mCallback = null;
    private static final int LEFT_MARGIN = Util.Div(-65);

    public interface ReloadCallback{
        void reload();
        boolean onkeyListener(View v, int keyCode, KeyEvent event);
    }

    public ServerErrorLayout(Context context, ReloadCallback callback) {
        super(context);
        addConnectBtn();
        addTitle();
        addTips1();
        addShowFocus();
        setReloadCallBack(callback);
    }

    public void setReloadCallBack(ReloadCallback callBack){
        this.mCallback = callBack;
    }

    private void addTitle(){
        FrameLayout titleLayout = new FrameLayout(getContext());
        titleLayout.setPadding(0, 0, Util.Div(45), 0);
        FrameLayout.LayoutParams title_p = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, Util.Div(106));
        addView(titleLayout, title_p);
    }

    private void addShowFocus(){
        mShowFocus = new TextView(getContext());
        mShowFocus.setTextColor(getResources().getColor(R.color.title_focus));
        mShowFocus.setText(R.string.load_refresh_tv);
        mShowFocus.setTextSize(Util.Dpi(SkyTextSize.t_4));
        mShowFocus.setGravity(Gravity.CENTER);
        mShowFocus.setBackgroundResource(R.drawable.sky_unfocus_bg);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                Util.Div(300), Util.Div(140),Gravity.CENTER_HORIZONTAL);
        params.topMargin = Util.Div(590);
        params.leftMargin = LEFT_MARGIN;
        addView(mShowFocus,params);
    }

    private void addTips1() {
        mTipsTxt1 = new TextView(getContext());
        mTipsTxt1.setTextColor(getResources().getColor(R.color.c_4));
        mTipsTxt1.setTextSize(Util.Dpi(SkyTextSize.t_4));
        mTipsTxt1.setGravity(Gravity.CENTER);
        mTipsTxt1.setText(R.string.load_content_error);
        mTipsTxt1.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.sky_status_icon_error,0,0);
        mTipsTxt1.setCompoundDrawablePadding(Util.Div(38));
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_HORIZONTAL);
        params.leftMargin = LEFT_MARGIN;
        params.topMargin = Util.Div(300);
        addView(mTipsTxt1, params);
    }

    private void addConnectBtn() {
        mConnectBtn = new Button(getContext());
        mConnectBtn.setOnClickListener(mClickListener);
        mConnectBtn.setBackground(null);
        mConnectBtn.setOnFocusChangeListener(focusListener);
        mConnectBtn.setOnKeyListener(mKeyListener);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Util.Div(1624), Util.Div(846));
        params.topMargin = Util.Div(128);
        params.leftMargin = LEFT_MARGIN;
        addView(mConnectBtn, params);
    }

    public void setFocus(){
        if(mConnectBtn != null)
            mConnectBtn.requestFocus();
    }

    View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mCallback != null)
                mCallback.reload();
        }
    };

    View.OnKeyListener mKeyListener = new View.OnKeyListener(){

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            return mCallback.onkeyListener(v, keyCode, event);
        }
    };

    View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus){
                mShowFocus.setBackgroundResource(R.drawable.sky_focus_bg);
                mShowFocus.setTextColor(getResources().getColor(R.color.ffffff));
            }else{
                mShowFocus.setBackgroundResource(R.drawable.sky_unfocus_bg);
                mShowFocus.setTextColor(getResources().getColor(R.color.c_4));
            }
        }
    };
}
